class Solution {
public:
    vector<vector<int>> ret;

    bool check[9] = { false };
    void dfs(vector<int>& nums, vector<int> path)
    {
        if(path.size() == nums.size())
        {
            ret.push_back(path);
            return;
        }
        for(int i = 0; i < nums.size(); i++)
        {
            if(check[i] == false && (i == 0 || nums[i] != nums[i - 1] || check[i - 1] == true))
            {
                check[i] = true;
                path.push_back(nums[i]);
                dfs(nums, path);
                check[i] = false;
                path.pop_back();
            }
        }
    }

    vector<vector<int>> permuteUnique(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        dfs(nums, vector<int>());
        return ret;
    }
};